/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanterre.annuaire.papier;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.nanterre.annuaire.bd.BDInteraction;

import com.nanterre.annuaire.model.*;
import java.awt.Color;
import java.io.*;

import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author illamhat
 */
public class AnnuairePDF {

    private String link;
    BDInteraction bdi = new BDInteraction();

    public AnnuairePDF(String link){
        this.link= link;
        
    }
    
    public AnnuairePDF(String link, String titre, String recherche, int parentID, java.util.List<Integer> liste) throws SQLException, ClassNotFoundException {
        this.link = link;



        try {
            Document document = new Document(PageSize.A4);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(link));
            document.open();

            document.addTitle(titre);
            writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);

            com.lowagie.text.List fileListe = new com.lowagie.text.List();

            if (recherche.equalsIgnoreCase(TypeElement.DOMAINE.getLabel())) {
                fileListe = this.getDomaineById(parentID);
            }
            if (recherche.equalsIgnoreCase(TypeElement.MENTION.getLabel())) {
                fileListe = this.getAllDiplomesByIdMention(parentID);
            }
            if (recherche.equalsIgnoreCase(TypeElement.DIPLOME.getLabel())) {
                fileListe = this.getAllParcoursByIdDiplome(parentID);
            }
            if (recherche.equalsIgnoreCase(TypeElement.PARCOURS.getLabel())) {
                fileListe = this.getAllUeEcByIdParcours(parentID);
            }
            if ((recherche.equalsIgnoreCase(TypeElement.UE.getLabel()) || recherche.equalsIgnoreCase(TypeElement.EC.getLabel())) 
                    && (liste != null && !liste.isEmpty())) {
                for (int i = 0; i < liste.size(); i++) {
                    com.lowagie.text.List tmp = new com.lowagie.text.List();
                    tmp = this.getAllUeEcByIdParcours(liste.get(i));
                    fileListe.add(tmp);
                }
            }




            document.add(fileListe);
            document.close();

        } catch (DocumentException de) {
            Logger.getLogger(BDInteraction.class.getName()).log(Level.SEVERE, null, de);
        } catch (IOException ioe) {
            Logger.getLogger(BDInteraction.class.getName()).log(Level.SEVERE, null, ioe);
        }


    }

    public com.lowagie.text.List getDomaineById(int idDomaine) {

        Domaine domaine = new Domaine();
        domaine = bdi.getDomaineById(idDomaine);

        com.lowagie.text.List listeDomaine = new com.lowagie.text.List();
        if (domaine != null) {
            listeDomaine.add(this.getAllMentionsByIdDomaine(domaine.getId()));
        }
        
        return listeDomaine;
    }

    public com.lowagie.text.List getAllMentionsByIdDomaine(int idDomaine) {
        java.util.List<Mention> mentions = new ArrayList<Mention>();
        mentions = bdi.getAllMentionsByIdDomaine(idDomaine);
        com.lowagie.text.List listeMention = new com.lowagie.text.List();
        ListItem listItem;
        listItem = new ListItem("[" + bdi.getDomaineById(idDomaine).getCodeApogee() + "] : " + bdi.getDomaineById(idDomaine).getLibelleComplet(),
                FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLD, Color.RED));
        listeMention.add(listItem);

        for (int i = 0; i < mentions.size(); i++) {
            listeMention.add(this.getAllDiplomesByIdMention(mentions.get(i).getId()));
        }
        
        return listeMention;
    }

    public com.lowagie.text.List getAllDiplomesByIdMention(int idMention) {
        java.util.List<Diplome> diplomes = new ArrayList<Diplome>();
        diplomes = bdi.getAllDiplomesByIdMention(idMention);
        com.lowagie.text.List listeDiplome = new com.lowagie.text.List();
        ListItem listItem;
        listItem = new ListItem("mention : " + bdi.getMentionsByIdMention(idMention).getLibelleComplet(), FontFactory.getFont(FontFactory.TIMES, 12, Font.BOLD, Color.GRAY));
        listeDiplome.add(listItem);
        for (int i = 0; i < diplomes.size(); i++) {

            listeDiplome.add(this.getAllParcoursByIdDiplome(diplomes.get(i).getId()));
        }
        
        return listeDiplome;
    }

    public com.lowagie.text.List getAllParcoursByIdDiplome(int idDiplome) {
        java.util.List<Parcours> parcours = new ArrayList<Parcours>();
        parcours = bdi.getAllParcoursByParentId(idDiplome);
        com.lowagie.text.List listeParcours = new com.lowagie.text.List();
        ListItem listItem;
        listItem = new ListItem("diplome : " + bdi.getDiplomeById(idDiplome).getLibelleComplet());
        listeParcours.add(listItem);
        for (int i = 0; i < parcours.size(); i++) {

            listeParcours.add(this.getAllUeEcByIdParcours(parcours.get(i).getId()));

        }
        
        return listeParcours;
    }

    public com.lowagie.text.List getAllUeEcByIdParcours(int idParcours) {
        java.util.List<UE> listeUe = new ArrayList<UE>();
        listeUe = bdi.getAllUeByParentId(idParcours);
        com.lowagie.text.List listeUeEC = new com.lowagie.text.List();
        ListItem listItem;
        listItem = new ListItem("parcours : " + bdi.getParcoursById(idParcours).getLibelleComplet() + "\n\n",
                FontFactory.getFont(FontFactory.TIMES, 12, Font.NORMAL, Color.BLACK));
        listeUeEC.add(listItem);
        if (!listeUe.isEmpty()) {
            PdfPTable table = createTable(listeUe);
            if (table != null) {
                Paragraph par = new Paragraph();
                par.add(table);
                listItem = new ListItem(par);
                listeUeEC.add(listItem);
            }
        }
       
        return listeUeEC;
    }

    public PdfPTable createTable(java.util.List<UE> liste) {

        if (liste.isEmpty()) {
            return null;
        }
        int[] t = new int[10];

        for (int i = 0; i < t.length; i++) {
            t[i] = 0;
        }

        for (int i = 0; i < liste.size(); i++) {
            switch (liste.get(i).getSemestre()) {
                case 0:
                    t[0] = t[0] + 1;
                    break;
                case 1:
                    t[1] = t[1] + 1;
                    break;
                case 2:
                    t[2] = t[2] + 1;
                    break;
                case 3:
                    t[3] = t[3] + 1;
                    break;
                case 4:
                    t[4] = t[4] + 1;
                    break;
                case 5:
                    t[5] = t[5] + 1;
                    break;
                case 6:
                    t[6] = t[6] + 1;
                    break;
                case 7:
                    t[7] = t[7] + 1;
                    break;
                case 8:
                    t[8] = t[8] + 1;
                    break;
                case 9:
                    t[9] = t[9] + 1;
                    break;
                default:
                    break;

            }
        }




        PdfPTable table = new PdfPTable(4);
        try {
            table.setWidths(new float[]{20f, 20f, 5f, 5f});
        } catch (DocumentException ex) {
            Logger.getLogger(AnnuairePDF.class.getName()).log(Level.SEVERE, null, ex);
        }
        // the cell object
        PdfPCell cell;


        int cmp = 0;
        for (int i = 0; i < t.length; i++) {
            if (t[i] != 0) {


                cell = new PdfPCell(new Phrase("Semestre " + String.valueOf(i), FontFactory.getFont(FontFactory.defaultEncoding, 8, Font.NORMAL, Color.BLACK)));
                cell.setColspan(4);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(Color.GRAY);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase("UE", FontFactory.getFont(FontFactory.defaultEncoding, 8, Font.NORMAL, Color.BLACK)));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(Color.lightGray);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase("EC", FontFactory.getFont(FontFactory.defaultEncoding, 8, Font.NORMAL, Color.BLACK)));
                cell.setColspan(3);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(Color.lightGray);
                table.addCell(cell);

                int cmp2 = 0;
               
                while (cmp2 < t[i]) {

                    float nbCredits = bdi.getCreditsUE(liste.get(cmp).getId());
                    
                    

                    java.util.List<Ec> listeEc = new ArrayList<Ec>();
                    listeEc = bdi.getAllEcInfoByParentId(liste.get(cmp).getId(), liste.get(cmp).getParentID());
                    
                    cell = new PdfPCell(new Phrase(liste.get(cmp).getLibelleComplet() + "\n[" + liste.get(cmp).getCodeApogee() + "]\n\n credits : " + String.valueOf(nbCredits) + "\n", FontFactory.getFont(FontFactory.defaultEncoding, 6, Font.NORMAL, Color.BLACK)));
                    liste.get(cmp).setListeEc(listeEc);

                    int cmpElementEC = 0;
                    for (int k = 0; k < listeEc.size(); k++) {
                        cmpElementEC = cmpElementEC + listeEc.get(k).getInfos().size();
                    }

                    cell.setRowspan(cmpElementEC);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    table.addCell(cell);


                    for (int k = 0; k < listeEc.size(); k++) {

                        String phrase = listeEc.get(k).getLibelleComplet() + "\n[" + listeEc.get(k).getCodeApogee() + "]";
                        if (listeEc.get(k).getNomResponsable() != null && listeEc.get(k).getPrenomResponsable() != null) {
                            phrase = phrase + "\n resp. : " + listeEc.get(k).getNomResponsable() + " " + listeEc.get(k).getPrenomResponsable();
                        }

                        cell = new PdfPCell(new Phrase(phrase, FontFactory.getFont(FontFactory.defaultEncoding, 6, Font.NORMAL, Color.BLACK)));
                        cell.setRowspan(listeEc.get(k).getInfos().size());
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        table.addCell(cell);


                        for (int m = 0; m < listeEc.get(k).getInfos().size(); m++) {
                            if ("Credits".equalsIgnoreCase(listeEc.get(k).getInfos().get(m).getNom())) {
                                cell = new PdfPCell(new Phrase(String.valueOf(listeEc.get(k).getInfos().get(m).getNombre()) + " ECTs", FontFactory.getFont(FontFactory.COURIER_BOLD, 6, Font.NORMAL, Color.BLACK)));
                                cell.setColspan(2);
                                table.addCell(cell);

                            } else {
                                cell = new PdfPCell(new Phrase(listeEc.get(k).getInfos().get(m).getNom() + " : " + String.valueOf((int) listeEc.get(k).getInfos().get(m).getNombre()) + " h", FontFactory.getFont(FontFactory.COURIER, 6, Font.NORMAL, Color.BLACK)));
                                cell.setColspan(2);
                                table.addCell(cell);

                            }

                        }

                    }

                    cmp++;
                    cmp2++;
                }
            }
        }
        
        return table;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }
}

// 3154